﻿using System;
using System.Collections.Generic;
using System.Text;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.IBatisRepository;
using OneMG.Phoenix.Respository;
using System.Data;
using OneMG.Phoenix.Entity;

namespace OneMG.Phoenix.Service
{
    public class RightsService : IRightsService
    {
        IRights rights = new RightsRepository();

        IRightsTypeService rightsType = new RightsTypeService();

        public System.Int32 AddRights(RightsModel model)
        {
            ERights entity = ConvertModelToEntity(model);

            return rights.AddRights(entity);
        }

        public void UpdateByRightID(RightsModel model)
        {
            ERights entity = ConvertModelToEntity(model);

            rights.UpdateByRightID(entity);

        }

        public void DeleteByRightID(System.Int32 rightID)
        {

            rights.DeleteByRightID(rightID);

        }

        public RightsModel GetByRightID(System.Int32 rightID)
        {

            ERights entity = rights.GetByRightID(rightID);

            RightsModel model = ConvertEntityToModel(entity);


            return model;
        }

        public IList<RightsModel> GetByRightTypeID(System.Int32 rightTypeID)
        {
            IList<ERights> entityList = rights.GetByRightTypeID(rightTypeID);
            if (entityList == null)
                return null;

            IList<RightsModel> modelList = new List<RightsModel>();

            foreach (var item in entityList)
            {
                RightsModel model = ConvertEntityToModel(item);
                modelList.Add(model);
            }
            return modelList;

        }

        public string GetRightNameByUserID(System.Int32 userID)
        {
            return rights.GetRightNameByUserID(userID);
        }


        public IList<RightsAndTypeModel> GetRightAndType()
        {

            IList<RightsAndTypeModel> rightsAndTypeList = new List<RightsAndTypeModel>();

            IList<RightsTypeModel> rightsTypeList = rightsType.GetAll();

            foreach (var item in rightsTypeList)
            {
                RightsAndTypeModel model = new RightsAndTypeModel()
                {
                    Rights = GetByRightTypeID(item.TypeID),
                    RightsType = rightsType.GetByTypeID(item.TypeID)
                };
                rightsAndTypeList.Add(model);
            }


            return rightsAndTypeList;
        }


        #region
        public RightsModel ConvertEntityToModel(ERights entity)
        {
            if (entity == null)
            {
                return null;
            }
            RightsModel model = new RightsModel();
            model.RightID = entity.RightID;
            model.RightName = entity.RightName;
            model.RightTypeID = entity.RightTypeID;
            model.RightDisplayName = entity.RightDisplayName;
            model.RightMemo = entity.RightMemo;
            model.SystemFlag = entity.SystemFlag;
            model.CreateTime = entity.CreateTime;
            model.CreateUser = entity.CreateUser;
            model.UpdateTime = entity.UpdateTime;
            model.UpdateUser = entity.UpdateUser;
            return model;
        }

        public ERights ConvertModelToEntity(RightsModel model)
        {
            if (model == null)
            {
                return null;
            }
            ERights entity = new ERights();
            entity.RightID = model.RightID;
            entity.RightName = model.RightName;
            entity.RightTypeID = model.RightTypeID;
            entity.RightDisplayName = model.RightDisplayName;
            entity.RightMemo = model.RightMemo;
            entity.SystemFlag = model.SystemFlag;
            entity.CreateTime = model.CreateTime;
            entity.CreateUser = model.CreateUser;
            entity.UpdateTime = model.UpdateTime;
            entity.UpdateUser = model.UpdateUser;
            return entity;
        }
        #endregion


    }
}